package com.jinyi.wwdeng.ui.config.fragment

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.FragmentActivity
import com.jinyi.base.base.BaseActivity
import com.jinyi.base.base.BaseFragment
import com.jinyi.base.utils.HHLog
import com.jinyi.wwdeng.databinding.FragmentFaceRecognitionBinding
import com.jinyi.wwdeng.ui.config.act.FaceCameraAct
import com.jinyi.wwdeng.ui.permission.PermissionListener
import com.jinyi.wwdeng.ui.permission.PermissionUtils

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/23 10:31
 * @Annotation：人脸识别
 */
class FaceRecognitionFragment: BaseFragment() {

    lateinit var binding: FragmentFaceRecognitionBinding

    override fun getRootView(inflater: LayoutInflater): View {
        binding = FragmentFaceRecognitionBinding.inflate(inflater)
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        //开始
        binding.btnStartTaking.setOnClickListener {
            val act = context as BaseActivity
            if (PermissionUtils.isAuthorized(act,"android.permission.CAMERA")){
                faceCameraResultLauncher.launch(Intent(context,FaceCameraAct::class.java))
            } else {
                PermissionUtils.requestPermission(act,"android.permission.CAMERA",object : PermissionListener {
                    override fun requestResult(isFlog: Boolean) {
                        HHLog.e("requestResult>>>>>>>>>>>>>>$isFlog")
                        if (isFlog){
                            faceCameraResultLauncher.launch(Intent(context,FaceCameraAct::class.java))
                        } else {
                            showToast("相机打开失败")
                        }
                    }
                })
            }
        }
    }

    override fun initData() {
    }

    /**
     * 人脸拍照
     */
    private val faceCameraResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
        when (result.resultCode) {
            FragmentActivity.RESULT_OK -> {
                HHLog.e(">>>>>>>>>人脸拍照后返回")
            }
        }
    }
}